package utils;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLDocument.Iterator;
import javax.swing.text.html.HTMLEditorKit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.omg.CORBA.portable.InputStream;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;


public class GoogleMapEditorPan extends JEditorPane{
	private static final long serialVersionUID = 1L;

	private static final Component DeliveryFormContainergoogleMap = null;
	
    private String roadmap = "roadmap";
    public final String viewTerrain = "terrain";
    public final String viewSatellite = "satellite";
    public final String viewHybrid = "hybrid";
    public final String viewRoadmap = "roadmap";
    public Document dom;
    private URLConnection urlConnection;
    private BufferedReader in;
    
    /**
     * Constructeur: initialisation du EditorKit
     */
    public GoogleMapEditorPan() {
        HTMLEditorKit kit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) kit.createDefaultDocument();
        setEditable(false);
        setContentType("text/html");
        setEditorKit(kit);
        setDocument(htmlDoc);
    }

    /**
     * Fixer le type de vue
     * @param roadMap
     */
    public void setRoadmap(String roadMap) {
        this.roadmap = roadMap;
    }
    
    private String getParsedXML(Document doc){
		
    	String polyline = "";
    	
		
		
		return polyline;
	}
    
    public String setDirection(String emplacement, String addressRest, String addressTo) throws Exception {
    	
    	String polyLine = "";
    	
    	String directions = "http://maps.google.com/maps/api/directions/xml?";
    	directions += "&origin=" + URLEncoder.encode(emplacement,"UTF-8");
        directions += "&waypoints=" + URLEncoder.encode(addressRest,"UTF-8");
        directions += "&destination=" + URLEncoder.encode(addressTo,"UTF-8");
        directions += "&sensor=false";
        
        	System.out.println("*******************URL******************************");
        	System.out.println(directions);
        	System.out.println("");
        
        	URL googleMap = new URL(directions);
        	
        	urlConnection = googleMap.openConnection();
        	
        
        	try{
        	in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        	}
        	catch (Exception e) {
            	 JOptionPane.showMessageDialog(DeliveryFormContainergoogleMap,"Please check your Internet connection and try again","WARNING",JOptionPane.ERROR_MESSAGE);
            
          	}
        	String inputLine;
        	System.out.println("*******************XML******************************");	
    			while ((inputLine = in.readLine()) != null){
    				System.out.println(inputLine);
    				if(inputLine.trim().equals("<overview_polyline>") == true){
    					polyLine = in.readLine();
    					System.out.println(polyLine);
    				}
    			}
    			in.close();
        
        return polyLine;
    }
    
    
    public void setMap(String emplacement, String addressRest, String addressTo, Integer width, Integer height) throws Exception {
    	
    	String polyLineData;

    	polyLineData = setDirection(emplacement, addressRest, addressTo);
    	
    	polyLineData = polyLineData.replaceAll("<points>", "");
    	polyLineData = polyLineData.replaceAll("</points>", "");
    	polyLineData = polyLineData.trim();
    	
    	System.out.println("****************POLYLINE*************************");
    	System.out.println(polyLineData);
    	System.out.println("*************************************************");
    	
    	String url = "http://maps.google.com/maps/api/staticmap?";
        url += "size=" + width + "x" + height;
        
        url += "&markers=color:green|size:mid|" + URLEncoder.encode(emplacement,"UTF-8") + ",quebec,canada" + 
        	"|" + URLEncoder.encode(addressRest,"UTF-8") + "|" + URLEncoder.encode(addressTo,"UTF-8") + "|" ;
        
        url += "&path=color:0x0000ff|weight:5|enc:"+ polyLineData;
        
        url += "&sensor=false";

        System.out.println(url);
        
        String html = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
        html += "<html><head></head><body>";
        html += "<img src='" + url + "'>";
        html += "</body></html>";
        
        setText(html);
    }

}